<!-- eslint-disable no-unreachable -->
<template>
    <div>
  
      <el-container>
        <!-- 搜索栏 -->
        <el-header>
          <el-row :gutter="20" style="padding: 5px 20px;">
            <el-col :span="6"><el-input v-model.trim="searchModel.pipeName" placeholder="项目名称" clearable @keyup.enter.native="getPipeList" /></el-col>
            <el-col :span="6"><el-button type="primary" round icon="el-icon-search" @click="getPipeList">搜索</el-button></el-col>
            <el-col align="right" :span="6"><el-button type="primary" round icon="el-icon-plus" @click="handleDialogVisible(null)">新增</el-button></el-col>
          </el-row>
        </el-header>
  
        <!-- 结果列表 -->
        <el-main>
          <el-table :data="list" style="width: 100%" :header-cell-style="centerHeaderStyle" :cell-style="centerCellStyle">
            <el-table-column type="index" label="序号" width="100">
              <template slot-scope="scope">
                {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
              </template>
            </el-table-column>
            <el-table-column prop="pipeNumber" label="型号编码" width="150" />
            <el-table-column prop="pipeName" label="管道名称" width="100" />
            <el-table-column prop="pipeModelName" label="管道模型" />
            <el-table-column prop="pipeItemName" label="所属项目" />
            <el-table-column prop="pipeSensorName" label="绑定传感器" />
            <el-table-column prop="pipeGenerateTime" label="生产日期" />
            <el-table-column prop="pipeCreateTime" label="出场日期" />
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  @click="handleDialogVisible(scope.row.id)"
                >修改</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="deletePipe(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-main>
  
        <!-- 分页 -->
        <el-footer>
          <el-pagination
            :current-page="searchModel.pageNo"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="searchModel.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalPage"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-footer>
      </el-container>
  
      <!-- 修改框 -->
      <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearForm">
        <el-form ref="formRef" :model="form" :rules="rules">
          <el-form-item prop="pipeNumber" label="管道编码" :label-width="formLabelWidth">
            <el-input v-model="form.pipeNumber" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="pipeName" label="管道名称" :label-width="formLabelWidth">
            <el-input v-model="form.pipeName" autocomplete="off" />
          </el-form-item>
          <el-form-item prop="pipeModel" label="管道模型" :label-width="formLabelWidth">
            <el-select v-model="form.pipeModel" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in modelNameForm"
                :key="item.id"
                :label="item.pipeType"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="pipeItem" label="所属项目" :label-width="formLabelWidth">
            <el-select v-model="form.pipeItem" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in itemNameForm"
                :key="item.itemId"
                :label="item.itemName"
                :value="item.itemId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item prop="pipeSensor" label="绑定传感器" :label-width="formLabelWidth">
            <el-select v-model="form.pipeSensor" placeholder="请选择" style="width: 100%;">
              <el-option
                v-for="item in sensorNameForm"
                :key="item.id"
                :label="item.sensorName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        
          <el-form-item prop="pipeGenerateTime" label="生产日期" :label-width="formLabelWidth">
            <el-date-picker type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                v-model="form.pipeGenerateTime"></el-date-picker>
          </el-form-item>
          <el-form-item prop="pipeCreateTime" label="出产日期" :label-width="formLabelWidth">
            <el-date-picker type="datetime" format="yyyy-MM-dd HH:mm:ss" value-format="yyyy-MM-dd HH:mm:ss"
                v-model="form.pipeCreateTime"></el-date-picker>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleSave">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </template>
  
  <script>
  import pipeRequest from '@/api/pipeManage'
  import modelPipeRequest from '@/api/modelPipeManage'
  import itemRequest from '@/api/itemManage'
  import sensorRequest from '@/api/sensorManage'
  export default {
    data() {
      return {
        totalPage: 0,
        modelNameForm: {},
        itemNameForm: {},
        sensorNameForm: {},
        searchModel: {
          pipeName: null,
          pageNo: 1,
          pageSize: 10
        },
        list: [],
        dialogFormVisible: false, // 对话框显示隐藏
        form: {
          
        },
        formLabelWidth: '150px',
        title: '',
        rules: {
          orgDept: [
            { required: true, message: '请输入角色名', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getPipeList()
      this.getModelNameForm()
      this.getItemlNameForm()
      this.getSensorlNameForm()
    },
    methods: {
      getModelNameForm() {
        modelPipeRequest.getAllModelPipeList().then(res => {
          this.modelNameForm = res.data
        })
      },
      getItemlNameForm() {
        itemRequest.getAllItemList().then(res => {
          this.itemNameForm = res.data
        })
      },
      getSensorlNameForm() {
        sensorRequest.getAllSensorList().then(res => {
          this.sensorNameForm = res.data
        })
      },
      handleSizeChange(pageSize) {
        this.searchModel.pageSize = pageSize
        this.getPipeList()
      },
      handleCurrentChange(pageNo) {
        this.searchModel.pageNo = pageNo
        this.getPipeList()
      },
      getPipeList() {
        // then是调完后的回调方法
        // 在then中把数据传回过来, total 和 list要复制
        pipeRequest.getPipeList(this.searchModel).then(response => {
          this.totalPage = response.data.totalPage
          this.list = response.data.rows
        })
      },
      handleDialogVisible(id) {
        if (id == null) {
          this.title = '新增部门'
        } else {
          this.title = '修改部门'
          pipeRequest.getPipeById(id).then(response => {
            this.form = response.data
          })
        }
  
        this.dialogFormVisible = true
      },
      clearForm() {
        this.form = {}
        this.$refs.formRef.clearValidate()
      },
      handleSave() {
        // 触发表单验证
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            pipeRequest.savePipe(this.form).then(response => {
              // 成功提示
              this.$message({
                message: response.message,
                type: 'success'
              })
              // 关闭表格
              this.dialogFormVisible = false
              // 刷新表格
              this.getPipeList()
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deletePipe(pipe) {
        this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          pipeRequest.deletePipeById(pipe.id).then(response => {
            this.$message({
              type: 'success',
              message: response.message
            })
            this.getPipeList()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      timestampToTime(timestamp) {
        timestamp = timestamp ? timestamp : null;
        let date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
        let h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
        let m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
        let s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        return Y + M + D + h + m + s;
      },
      centerHeaderStyle({ row, column, rowIndex, columnIndex }) {
        return { 'text-align': 'center' };
      },
      centerCellStyle({ row, column, rowIndex, columnIndex }) {
        return { 'text-align': 'center' };
      },
    }
  }
  </script>
  
  <style>
    .el-dialog .el-input {
      width: 70%;
    }
  </style>
  